<?php
/**
* d17操作
*/
class koubei
{
    //**** 定义私有变量 ****//

        // 用户信息相关
        private $status = 0;        // 状态

        // 定义错误信息
        private $errorMsg;          // 错误信息

        //  新闻默认选填参数
        private $postOptions = array(
        		'memid' => 279697,
        		'content' => '便宜是大家都知道的，个人感觉能很快找到自己想要去的地方更重要！',
        		'rank' => 4,
        		'srcdomain' => '27558d95b9e1a4a7ba8e7374d8bc61d3',
        		'phonecertify' => '{}',
        		'pics' => '[]',   	
        );
        
        private $request ;//请求头
        
       
    /**
     * 初始化函数
     * @param string $userName    用户账号
     * @param string $password    用户密码
     * @param string $userCookies 登录Cookies
     * @param array  $options     配置选项信息
     */
    function __construct($userName, $userCookies)
    {
        $this->request = new uaheader();
        
        print_f("当前设备 : ".$this->request->type.$this->request->browser."\n");
        $this->setCookie($userCookies) ?: $this->errorMsg = 'NotLogin';
    }

    /**
     * 获取上次操作的错误信息
     * @return string 错误信息描述
     */
    public function getErrorMsg()
    {
        return $this->errorMsg;
    }

    /**
     * 设置并验证用户Cookies
     * @param string $cookies 用户Cookies
     */
    public function setCookie($cookies)
    {
        // 验证登录的地址
        $url = 'https://koubei.baidu.com/';

        // 提交请求
        $retHtml = Get_Web_Contents($url, 'GET', '',$cookies);
        if(isset($retHtml['Error'])) {
            $this->errorMsg = 'cUrl请求时出错';
            return false;
        }
        // 判断是否已经登录
        $regEx = '#<div id="islogin" class="hidden">#';
        if(!preg_match_all($regEx, $retHtml['Body'], $formInfo)) {
         $this->userCookies = $cookies;
            $this->status      = 1;
            return true;
        } else {
            $this->status   = 0;
            $this->errorMsg = '账号未登录';
            return false;
         }
    }
		
    /**
     * 发布评语
     */
    public function postAddnew($title,$memid,$srcdomain)
    {
        //根据用户ua访问
        $parame = [
            'pc'  => [
                'Referer' => "https://koubei.baidu.com/s/$srcdomain.",//评论列表页
            ],
            'wap' => [
                'Referer' => "https://koubei.baidu.com/m2/mem/$srcdomain/comt/add",
            ],
        ];
        $parame = $parame[$this->request->type];
        
        $postData = array();
        $postData['memid']        = $memid;
        $postData['content']      = $title;
        $postData['rank']         = rand(4,5);
        $postData['pics']         = '[]';
        $postData['srcdomain']    = $srcdomain;
        $postData['phonecertify'] = '{}';
        
        $Head = $this->request->header;
        
        //合并请求头
        $Head = array_merge($Head,array(
            "Host: koubei.baidu.com",
            "Origin:https://koubei.baidu.com",
            "Referer:".$parame['Referer'],
            "X-Requested-With:XMLHttpRequest",
        ));
        
        $URL = "https://koubei.baidu.com/s/savecomtajax";
        
        //输入停留10秒
        sleep(10);
        $retHtml = Get_Web_Contents($URL, 'POST', http_build_query($postData), $this->userCookies,$Head);
        
        if(isset($retHtml['Error'])) {
            $this->errorMsg = 'cUrl请求时出错.';
            return false;
        }
       // 判断是否成功
        $json= $retHtml['Body'];
        $yes=json_decode($json, true)["status"];
        if($yes!=0){
        	$this->errorMsg = '评论失败.';
            return false;
        }else{
        	return true;
        }
    
    }
    /**
     * 点赞
     */
    public function postpraise($srcdomain)
    {
        //根据用户ua访问
        $parame = [
            'pc'  => [
                'capture' => "https://koubei.baidu.com/s/".$srcdomain."?tab=comt&praise=good",//评论列表页
                'regEx'   => '#data-id="(?<id>[^"]+)" data-type="1"#s',           //评论id正则
                'date_id' => 'comtid',           //评论id 表单
                'posturl' => 'https://koubei.baidu.com/comt/supportcomtajax',     //post 提交地址
            ],
            'wap' => [
                'capture' => "https://koubei.baidu.com/m2/mem/".$srcdomain."?praise=good",
                'regEx'   => '#praise" data-comt="(?<id>[^"]+)" data-ctype="1"#s',
                'date_id' => 'cid',           //评论id 表单
                'posturl' => 'https://koubei.baidu.com/m/addsupportajax',
            ],
        ];
        $parame = $parame[$this->request->type];
        
        $Head = $this->request->header;
        
        //获取data_id
        $retHtml = Get_Web_Contents($parame['capture'], 'GET', '', $this->userCookies ,$Head);
             
        if(!preg_match_all($parame['regEx'], $retHtml['Body'],$val)){
            $this->errorMsg = '提取id出错.';
            return false;
        }
        
        //合并请求头
        $Head = array_merge($Head,array(
                "Host: koubei.baidu.com",
                "Origin:https://koubei.baidu.com",
                "Referer:".$parame['capture'],
                "X-Requested-With:XMLHttpRequest",
        ));
        
        $id  = array_unique($val['id']);
        
        //点赞表单
        $post = array(
            'type'  => 1,
        );
        
        //wap 多个参数
        if($this->request->type == 'wap'){
            $post['ctype'] = 1;
        }
        
        $i = 0;
        foreach ($id as $v){
            
            if(rand(0,1)){
                continue;
            }
            
            //评论id
            $post[$parame['date_id']] = $v;
            
            $retHtml = Get_Web_Contents($parame['posturl'], 'POST', http_build_query($post), $this->userCookies,$Head); 
            
            //点赞频繁return
            $status = json_decode($retHtml['Body'], true)["status"];
            if($status != 0){
                print_f('cUrl请求时出错.');
                return false;
            }
            $i++;
            print_f("点赞 $i\n");
            //点赞间隔1秒
            sleep(3);
        }
        
        if(isset($retHtml['Error'])) {
            print_f('cUrl请求时出错.');
            $this->errorMsg = 'cUrl请求时出错.';
            return false;
        }
        // 判断是否成功
        $json = $retHtml['Body'];
        $yes  = json_decode($json, true)["status"];
        if($yes!=0){
            $this->errorMsg = '点赞失败.';
            return false;
        }else{
            return true;
        }
    
    }
    
    /**
     * 获取评语列表
     */
    public function getPostList($memid)
    {
    	
    	//获取产品的链接
	    	$url = "https://koubei.baidu.com/s/".$memid."?tab=mycomt";    	
	        // 读取页面内容
	        $retHtml = Get_Web_Contents($url, 'GET', '', $this->userCookies);
	       
	        $retHtml['Body'] = str_ireplace(array("\r\n","\t", "\r","\n"), '', $retHtml['Body']);	  
	        // 拼接读取地址
	        $regEx = '#class="con" title="(?<name>[^"]+)"(.*?)<pre>(?<body>[^<]+)</pre>#s';
	        if(!preg_match($regEx, $retHtml['Body'], $val)){ 
	        	$this->errorMsg = '提取name和评语时出错.';
	        	return false;
	        }
	        //网名
	        $retList['name'] = $val["name"];
	        //评语
	        $retList['body'] = $val["body"];

        return $retList;
       
    }
}







